/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
    private Room room19;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player= new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room room1,room2,room3,room4,room5,room6,room7,room8,room9;
        Room room10,room11,room12,room13,room14,room15,room16,room17,room18;
        Room room20,room21,room22,room23,room24,room25,room26,room27;
        Thing apple,chicken,hamburger,corn,tomato,steak,treasure;
        // create the rooms
        apple = new Thing("苹果",10);
        chicken = new Thing("鸡腿",25);
        hamburger = new Thing("汉堡",15);
        corn = new Thing("玉米",15);
        tomato = new Thing("番茄",15);
        steak = new Thing("牛排",30);
        treasure = new Thing("宝箱",0);
        room1= new Room("书房",apple);
        room2= new Room("第一层楼梯口");
        room3= new Room("厨房",chicken);
        room4= new Room("出发点",apple);
        room5= new Room("阳台",corn);
        room6= new Room("储物间",tomato);
        room7= new Room("大厅",steak);
        room8= new Room("画室",apple);
        room9= new Room("会客室",apple);
        room10= new Room("金库",treasure);
        room11= new Room("画室",apple);
        room12= new Room("卧室",hamburger);
        room13= new Room("楼梯口");
        room14= new Room("厨房",steak);
        room15= new Room("阳台");
        room16= new Room("第二层楼梯口");
        room17= new Room("大厅",corn);
        room18= new Room("书房",apple);
        room19= new Room("终点");
        room20= new Room("书房",tomato);
        room21= new Room("卧室",steak);
        room22= new Room("大厅",apple);
        room23= new Room("阳台");
        room24= new Room("厨房",steak);
        room25= new Room("第三层楼梯口");
        room26= new Room("画室");
        room27= new Room("储物间",apple);
        
  
        // initialise room exits
        room1.setExit("后面",room4);
        room2.setExit("后面",room5);
        room2.setExit("上面",room13);
        room3.setExit("右边",room4);
        room3.setExit("后面",room6);
        room4.setExit("后面",room7);
        room4.setExit("前面",room1);
        room4.setExit("左边",room3);
        room5.setExit("后面",room9);
        room5.setExit("前面",room2);
        room6.setExit("前面",room3);
        room6.setExit("右边",room7);
        room7.setExit("前面",room4);
        room7.setExit("左边",room6);
        room7.setExit("右边",room8);
        room8.setExit("左边",room7);
        room8.setExit("右边",room9);
        room9.setExit("前面",room5);
        room9.setExit("左边",room8);
        room10.setExit("右边",room11);
        room11.setExit("右边",room12);
        room11.setExit("左边",room10);
        room12.setExit("后面",room14);
        room12.setExit("右边",room13);
        room12.setExit("左边",room11);
        room13.setExit("左边",room12);
        room13.setExit("下面",room2);
        room14.setExit("前面",room12);
        room14.setExit("后面",room17);
        room15.setExit("右边",room16);
        room16.setExit("右边",room17);
        room16.setExit("上面",room25);
        room17.setExit("右边",room18);
        room17.setExit("左边",room16);
        room17.setExit("前面",room14);
        room18.setExit("左边",room17);
        room19.setExit("右边",room20);
        room19.setExit("后面",room23);
        room20.setExit("左边",room19);
        room20.setExit("右边",room21);
        room21.setExit("后面",room24);
        room21.setExit("左边",room20);
        room21.setExit("右边",room22);
        room22.setExit("左边",room21);
        room23.setExit("前面",room19);
        room24.setExit("后面",room26);
        room24.setExit("前面",room21);
        room25.setExit("右边",room26);
        room25.setExit("下面",room16);
        room26.setExit("前面",room24);
        room26.setExit("左边",room25);
        room26.setExit("右边",room27); 
        room27.setExit("左边",room26);
        
        currentRoom = room4;  // start game outside
        
        
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            if(player.isDead())
            {
                   break;
            }
            Command command = parser.getCommand();
            finished = processCommand(command);
            
        }
        System.out.println("游戏结束，感谢你的游玩");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到我的城堡，人类");
        System.out.println("你被我困在城堡里面，你需要找到出口才能回去");
        System.out.println("输入help可以帮助你更清楚了解规则");
        System.out.println();
        System.out.println("这里是" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("我不清楚你的意思");
            return false;
        }
        Word commandWord =command.getCommandWord();
        switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:
                
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case LOOK:
                showRoomThing();
                break;
            case PICK:
                pickThing();
                break;
            case CHECK:
                checkBag();
                break;
            case EAT:
                eat(command);
                break;
                
        }
       
        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你不知道如何操作？你在寻求帮助？");
        System.out.println("这是一个三层迷宫,4*3的布局，每一层都有三个房间不可进");
        System.out.println("第三层有你想要的出口");
        System.out.println("有一个房间有宝箱,带着它去终点会有一个神秘奖励");
        System.out.println("当你的能量小于0时你将会死亡，你需要去寻找食物");
        System.out.println("你需要从第一层入口到达第三层的出口房间，才能通关");
        System.out.println();
        System.out.println("你可以输入的操作符有:");
        System.out.println("   go quit help pick look check eat");
        System.out.println("go+要去的方向（左边，右边，前面，后面，上面，下面）");
        System.out.println("quit：结束游戏");
        System.out.println("help:寻求帮助");
        System.out.println("pick：捡起食物");
        System.out.println("look：观察房间");
        System.out.println("check：检查背包");
        System.out.println("eat+食物，增加能量");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪里?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);
       
        
        if (nextRoom == null) {
            System.out.println("这里没有门!");
        }
        else {
            currentRoom = nextRoom;
            if(currentRoom==room19)
            {
                 System.out.println("恭喜你来到了出口");
                 System.out.println("我将会把你送回原本世界");
                 player.isGet();
            }
            else
            {
                System.out.println("你正在" + currentRoom.getDescription());
                currentRoom.printExits();
                player.step();
                
            }
        }
    }

    
    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("退出？");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    
    private void showRoomThing()
    {
        if(currentRoom.getThing()!=null)
            System.out.println("这个房间有一个"+currentRoom.getThing().getName());
        else
             System.out.println("房间里没东西");
    }  
    
    private void pickThing()
    {
        Thing thing =currentRoom.getThing();
        if(thing != null)
        {
            player.pick(thing);
            System.out.println("你获得了一个"+thing+"的东西" );
            currentRoom.remove();
        }
        else
            System.out.println("这里没有东西");
    } 
    
    private void checkBag()
    {
        player.check();
    } 
    
    private void eat(Command command)
    {
        if(!command.hasSecondWord()) {
           
            System.out.println("吃啥？");
            return;
        }
        String name = command.getSecondWord();
        player.eat(name);
        
    } 
}
